# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# Author     ：skyTree
# version    ：python 3.11
# Description：工具类的方法
"""
import json
import os

from django.conf import settings


def build_tree_menu(menus_list, pid=1):
    tree_menu = []
    for menu in menus_list:
        if menu['pid'] == pid:
            children = build_tree_menu(menus_list, menu['id'])
            if children:
                menu['children'] = children
            tree_menu.append(menu)
    return tree_menu


def build_tree_category(category_list, pid=None):
    tree_category = []
    for category in category_list:
        if category['pid'] == pid:
            children = build_tree_category(category_list, category['id'])
            if children:
                category['children'] = children
            tree_category.append(category)
    return tree_category


def get_image_url(request, imageName):
    """
    获取图片图片
    """
    # 1. 获取路径
    path = os.path.join(settings.MEDIA_ROOT, imageName)
    image_url = request.build_absolute_uri('/') + 'api/goods' + settings.MEDIA_URL + imageName
    # 2. 判断文件存不存在
    if os.path.isfile(path):
        return image_url
    return None
